package com.qxgl.qxglt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qxgl.qxglt.dao.IUsersDao;
import com.qxgl.qxglt.dto.UsersDTO;
import com.qxgl.qxglt.model.Roles;
import com.qxgl.qxglt.model.Users;
import com.qxgl.qxglt.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@ApiOperation("用户列表")
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private IUsersService usersService;
    @Resource
    private IUsersDao usersDao;

    @ApiOperation(value = "登录功能",notes = "登录功能")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "loginName",value = "登录名",required = true,paramType = "path"),
        @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "path")
    })
    @GetMapping(value = "/login/{loginName}/{password}")
    public Users login(@PathVariable("loginName") String loginName,@PathVariable("password") String password){
        return usersDao.findByLoginNameAndPassword(loginName, password);
    } 

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping
    public Map<String, Object> findAll() {
        List<Users> list = usersService.findAll();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", usersDao.count());
        map.put("data", list);
        return map;
    }

    @ApiOperation(value = "多条件分页查询用户",notes = "多条件分页查询用户")
    @PostMapping(value = "findPage")
    public Map<String, Object> findPage(UsersDTO dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Users> page = usersService.findPage(dto);
        map.put("code", 0);
        map.put("msg", "查询出功");
        map.put("count", page.getTotalElements());
        map.put("data", page.getContent());
        return map;
    }

    @ApiOperation(value = "更新用户信息", notes = "添加或修改")
    @PostMapping("/addOrEdit")
    public Users saveMember(@RequestBody Users u) {
        return usersService.saveUsers(u);
    }

    
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path")
    @DeleteMapping(value = "/{id}")
    public void del(@PathVariable("id") String id) {
         
        usersService.del(id);

    }

    @ApiOperation(value = "重置密码",notes = "重置密码")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping(value = "pwdRest/{id}")
    public int pwdRest(@PathVariable("id") String id){
        return usersDao.pwdRest(id);

    }

    @ApiOperation(value = "解锁",notes = "解锁")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping(value = "Lockout/{id}")
    public int Lockout(@PathVariable("id") String id){
        return usersDao.Lockout(id);
    }
    @ApiOperation(value = "锁定",notes = "锁定")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping(value = "noLockout/{id}")
    public int noLockout(@PathVariable("id") String id){
        return usersDao.noLockout(id);
    }
    @ApiOperation(value = "给用户设置角色")
    @PostMapping("/setRoles")
    public void setRole( String id,@RequestParam List<String> roleIdList){
        usersService.setRoles(id, roleIdList);
    }
    @ApiOperation(value = "移除用户角色",notes = "移除用户角色" )
    @PostMapping("/betach/{userId}/{roleIds}")
    public int betachUserRole( String userId,@RequestParam List<String> roleIds){
        return usersDao.detachUserByrolesList(userId, roleIds);
    }
    @ApiOperation(value = "查询用户角色",notes = "查询用户角色")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @GetMapping("/findRoleById/{userId}")
    public List<Roles> findUserRole(@PathVariable String id){
        return usersDao.findRoleById(id); 
    }







}